package bookstore.manager.impl;

import bookstore.bound.order.*;
import bookstore.manager.OrderManager;
import bookstore.model.Book;
import bookstore.model.Order;
import bookstore.model.OrderItem;
import bookstore.repository.BookRepository;
import bookstore.repository.OrderItemRepository;
import bookstore.repository.OrderRepository;
import bookstore.utils.SessionUtils;
import bookstore.utils.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.*;

@Service
public class OrderManagerImpl implements OrderManager {
    @Autowired
    protected OrderRepository orderRepository;
    @Autowired
    protected OrderItemRepository orderItemRepository;
    @Autowired
    protected BookRepository bookRepository;

    protected SessionUtils sessionUtils = new SessionUtils();

    @Override
    public Set<OrderDisp> getUserOrders(HttpSession session) {
        Long uid = sessionUtils.getProfile(session).map(UserInfo::getUserId).get();
        Set<Order> orders = orderRepository.findAllByUserid(uid);
        Set<OrderDisp> disp = new HashSet<>();
        for (Order order : orders) {
            disp.add(new OrderDisp(order));
        }
        return disp;
    }

    @Override
    public Order generateOrder(HttpSession session, ShoppingCart shoppingCart) {
        Set<CartItem> cart = shoppingCart.getCart();
        Set<OrderItem> items = new HashSet<>();

        Order order = new Order();
        for (CartItem item : cart) {
            OrderItem orderItem = new OrderItem();
            Optional<Book> opt = bookRepository.findById(item.getIsbn());

            System.out.println(opt.get().toString());

            orderItem.setBook(opt.get());
            orderItem.setCount(item.getCnt());
            orderItem.setPrice(opt.get().getPrice());

            items.add(orderItem);
        }
        order.setItems(items);
        return sessionUtils.setOrder(session, order).get();
    }

    @Override
    public Map submitOrder(HttpSession session, RecipientForm recipientForm) {
        Long uid = sessionUtils.getProfile(session).map(UserInfo::getUserId).get();
        Order order = sessionUtils.getOrder(session).get();

        order.setUserid(uid);
        order.setName(recipientForm.getName());
        order.setTel(recipientForm.getTel());
        order.setAddr(recipientForm.getAddr());
        Long orderId = orderRepository.save(order).getOrderid();

        order.getItems().forEach(orderItem -> {
            orderItem.setOrderid(orderId);
            orderItemRepository.save(orderItem);
        });

        Map<String, Boolean> rsp = new HashMap<>();
        rsp.put("success", Boolean.TRUE);

        return rsp;
    }

    @Override
    public Set<OrderDisp> getAllOrders() {
        ArrayList<Order> orders = (ArrayList<Order>) orderRepository.findAll();
        Set<OrderDisp> disp = new HashSet<>();
        for (Order order : orders) {
            disp.add(new OrderDisp(order));
        }
        return disp;
    }

    @Override
    public List<StatItem> getSaleStat() {
        return orderItemRepository.OrderSaleStat();
    }
}
